package com.wk.future;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CompleteFutureTest {

    public static void main(String[] args) {
        List<Integer> objects = new ArrayList<>();
        List<Integer> objects2 = new ArrayList<>();
        objects.add(3);
        final CompletableFuture<List<Integer>> future1 = CompletableFuture.supplyAsync(() -> {
            return Stream.of(1).collect(Collectors.toList());
        }).exceptionally(ex -> {
            System.out.println(ex);
            return null;
        });

        final CompletableFuture<List<Integer>> future2 = CompletableFuture.supplyAsync(() -> {
            method();
            return Stream.of(2).collect(Collectors.toList());
        }).exceptionally(ex -> {
            System.out.println(ex);
            return null;
        });

        List<CompletableFuture<List<Integer>>> futures = new ArrayList<>();
        futures.add(future1);
        futures.add(future2);

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        try{
            voidCompletableFuture.exceptionally(ex -> {
                System.out.println("voidCompletableFuture获取每日检查详情结果失败");
                return null;
            }).join();
            final List<Integer> integers = future1.get();
            final List<Integer> integers1 = future2.get();
            objects2.addAll(integers);
            objects2.addAll(integers1);
        } catch (Exception e){
            System.out.println("获取每日检查详情结果失败 ");
        }
        System.out.println("********************* end *********************");
        System.out.println(objects);
        System.out.println(objects2);


    }

    public static void method()  {
        try{
            Thread.sleep(10000);
        }catch (Exception e){
            e.printStackTrace();
        }
        int i =1;
        System.out.println(i/0);
    }

}
